package com.idbk.chargestation.activity.user;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.idbk.chargestation.AppContext;
import com.idbk.chargestation.R;
import com.idbk.chargestation.adapter.MessageAdapter;
import com.idbk.chargestation.bean.JsonMyMessage;
import com.idbk.chargestation.db.MessageDatabase;

import java.util.List;

/**
 * 我的消息
 * @author zhx
 *
 */
public class ActivityMyMessage extends Activity implements OnItemLongClickListener,OnClickListener{

	private Context mContext;
	private ListView mListView; 
	private List<JsonMyMessage> mList;//定义一个list集合
	private MessageDatabase mMessageDB;
	private LinearLayout mLayoutEmpty;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_message);

		mContext = ActivityMyMessage.this;

		setupView();

		setupData();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		setupData();
	}

	private void setupView() {
		mListView = (ListView)findViewById(R.id.fragment_listview_message_all);
		mListView.setOnItemLongClickListener(this);
		mMessageDB = new MessageDatabase(mContext);
		mLayoutEmpty= (LinearLayout)findViewById(R.id.tj_empty_view);
		
		findViewById(R.id.toolbar_back).setOnClickListener(this);
		findViewById(R.id.textview_empty).setOnClickListener(this);
	}

	private void setupData() {
		String username = AppContext.getInstance().getLoginUserName();
		mList = mMessageDB.selectMessage(username);
		//定义适配器
		MessageAdapter mAdapter = new MessageAdapter(mContext, mListView, mList, mMessageDB);
		mListView.setAdapter(mAdapter);
		if(mList.size() < 1) {
			mLayoutEmpty.setVisibility(View.VISIBLE);
			mListView.setVisibility(View.GONE);
		} else {
			mLayoutEmpty.setVisibility(View.GONE);
			mListView.setVisibility(View.VISIBLE);
			mAdapter.notifyDataSetChanged();
		}	
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.toolbar_back:
			finish();
			break;
		case R.id.textview_empty:
			final String username = AppContext.getInstance().getLoginUserName();
			new AlertDialog.Builder(mContext)
			.setTitle(R.string.common_dialog_tip)
			.setMessage("你确定要删除所有消息？")
			.setNegativeButton(R.string.common_cancel, null)
			.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mMessageDB.deleteAll(username);
					//重新载入
					setupData();
				}				
			})
			.create()
			.show();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mMessageDB.close();
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			final int position, long id) {
		new AlertDialog.Builder(mContext)
		.setTitle("提示")
		.setMessage("确定删除该条消息？")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				JsonMyMessage item = mList.get(position);
				mMessageDB.deleteMessage(item.id);
				setupData();//刷新数据
			}
		})
		.setNegativeButton("取消", null).show();
		return false;
	}

}
